import { Button } from "antd"
import Container from "../layout/Container"
import { useStoreDispatch, useStoreSelector } from "../hooks/useStore"
import { useCounterAction } from "../lib/store/counterSlice"
import { Link } from "react-router-dom"
import withPage, { useError } from "../hooks/withPage"
import { request } from "../lib/http/query"

type KGNB = {
    id: number,
    context: string,
    create_time: string
}[]

const Home = withPage(({ a, b, commit }) => {
    const count = useStoreSelector((state) => state.counterSlice.value)
    const dispatch = useStoreDispatch()
    const {
        increment
    } = useCounterAction()
    useError(() => {
        console.log(b.error.message)
        const time = setTimeout(() => {
            commit("b")(e => e)
        }, 3000)
        return () => clearTimeout(time);
    }, b.error)
    console.log(a.result)//三次响应，初始化、请求中、请求结束
    return (
        <Container>
            <Link to="/demo">
                toDemo
            </Link>
            <Button
                onClick={() => dispatch(increment())}
            >
                {count}
            </Button>
        </Container>
    )
}, {
    a: request<null, KGNB>('http://api.bug.hblzhd.com/api/admin/KGNB/getAll').setInitialValue([]),
    b: request<null, KGNB>('this is notFound url').setInitialValue([])
})
export default Home